package com.fy56.platform.goodsTracking.web.restful.api.controller;

import com.fy56.platform.goodsTracking.api.UploadApi;
import com.fy56.platform.goodsTracking.quartz.CarCollect;
import com.fy56.platform.goodsTracking.quartz.CollectingMoneyCollect;
import com.fy56.platform.goodsTracking.quartz.QuartzManager;
import com.fy56.platform.goodsTracking.web.restful.api.model.ResultModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("/goods/new")
public class GoodsController {
    private static final Logger log = LoggerFactory.getLogger(QueryController.class);
    @Autowired
    private UploadApi uploadApi;

    @Autowired
    private CarCollect carCollect;

    @Autowired
    private QuartzManager quartzManager;

    @Autowired
    private CollectingMoneyCollect collectingMoneyCollect;
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Object upload(@RequestParam(value = "begin") String begin,@RequestParam(value = "end")String end) {
        log.info("补传 begin:" + begin+" end:"+end);
        uploadApi.doPostGoods(begin,end);
        return new ResponseEntity(ResultModel.ok(), HttpStatus.OK);
    }
    @RequestMapping(value = "/pcmbadd", method = RequestMethod.POST)
    public Object pcmbadd(@RequestParam(value = "begin") String begin,@RequestParam(value = "end")String end) {
        log.info("mbadd 补传 begin:" + begin+" end:"+end);
        uploadApi.doUpload_Goods_CarBc(begin,end);
        return new ResponseEntity(ResultModel.ok(), HttpStatus.OK);
    }
    @RequestMapping(value = "/dskmxzadd", method = RequestMethod.POST)
    public Object dskmxzadd(@RequestParam(value = "begin") String begin,@RequestParam(value = "end")String end) {
        log.info("mbadd 补传 begin:" + begin+" end:"+end);
        uploadApi.doUpload_CollectingMoneyBC(begin,end);
        return new ResponseEntity(ResultModel.ok(), HttpStatus.OK);
    }

    @RequestMapping(value = "/pcmbsave", method = RequestMethod.POST)
    public Object pcmbaddbuchuan(@RequestParam(value = "begin") String begin,@RequestParam(value = "end")String end) {
        try {

            Calendar c = Calendar.getInstance();
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            Date beginDate = dateFormat1.parse(begin);
            Date endDate = dateFormat1.parse(end);
            Date date = beginDate;
            while (!date.equals(endDate)) {
                begin = dateFormat1.format(date)+" 00:00:00";
                end = dateFormat1.format(date)+" 23:59:59";
                log.info("pcmbsave 补传 begin:" + begin+" end:"+end);
                carCollect.addJobB(begin,end);
                c.setTime(date);
                c.add(Calendar.DATE, 1); // 日期加1天
                date = c.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return new ResponseEntity(ResultModel.ok(), HttpStatus.OK);
    }

    @RequestMapping(value = "/dskmxzsave", method = RequestMethod.POST)
    public Object dskmxzsave(@RequestParam(value = "begin") String begin,@RequestParam(value = "end")String end) {

        try {

            Calendar c = Calendar.getInstance();
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            Date beginDate = dateFormat1.parse(begin);
            Date endDate = dateFormat1.parse(end);
            Date date = beginDate;
            while (!date.equals(endDate)) {
                begin = dateFormat1.format(date)+" 00:00:00";
                end = dateFormat1.format(date)+" 23:59:59";
                log.info("dshk 补传 begin:" + begin+" end:"+end);
                collectingMoneyCollect.addJobB(begin,end);
                c.setTime(date);
                c.add(Calendar.DATE, 1); // 日期加1天
                date = c.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return new ResponseEntity(ResultModel.ok(), HttpStatus.OK);
    }
    @RequestMapping(value = "/mbsave", method = RequestMethod.POST)
    public Object mbsave(@RequestParam(value = "begin") String begin,@RequestParam(value = "end")String end) {

        try {

            Calendar c = Calendar.getInstance();
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            Date beginDate = dateFormat1.parse(begin);
            Date endDate = dateFormat1.parse(end);
            Date date = beginDate;
            while (!date.equals(endDate)) {
                begin = dateFormat1.format(date)+" 00:00:00";
                end = dateFormat1.format(date)+" 23:59:59";
                log.info("mb 补传 begin:" + begin+" end:"+end);
                quartzManager.addJobB(begin,end);
                c.setTime(date);
                c.add(Calendar.DATE, 1); // 日期加1天
                date = c.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return new ResponseEntity(ResultModel.ok(), HttpStatus.OK);
    }
}
